package com.medical.patient.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;

import com.medical.common.constant.RedisLoginKey;
import com.medical.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.medical.patient.entity.PatientTurnImgEntity;
import com.medical.patient.service.PatientTurnImgService;
import com.medical.common.utils.PageUtils;
import com.medical.common.utils.R;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * 
 *
 * @author wen
 * @email 1023076426@qq.com
 * @date 2021-08-19 22:32:06
 */
@RestController
@RequestMapping("patient/patientturnimg")
public class PatientTurnImgController {
    @Autowired
    private PatientTurnImgService patientTurnImgService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 轮播图列表
     */
    @RequestMapping("/list")
    public R list(HttpServletResponse response,HttpSession session,@RequestParam Map<String, Object> params){
        String token = (String) params.get("token");
        //有token的值，说明已经登陆过了
        if(StringUtils.isNotEmpty(token)){
            Set<String> keys = redisTemplate.keys(RedisLoginKey.PATIENT_ROLE.keyPrefix+"*");
            if(keys!=null&&keys.size()>0){//有人正在登录
                for (String key : keys) {
                    String[] split = key.split(":");
                    if(split[2].equals(token)){//验证令牌通过
                        //开始鉴权
                        Claims claims = jwtUtil.parseJWT(token);
                        String role = (String) claims.get("roles");
                        if(role.equals("患者")){
                            String s = redisTemplate.opsForValue().get(RedisLoginKey.PATIENT_ROLE.keyPrefix + split[2]);//获得用户的信息
                            session.setAttribute("loginuser",s);
                            PageUtils page = patientTurnImgService.queryPage(params);
                            return R.ok().put("page", page);
                        }else {
                            return R.error("权限不足，无法访问");
                        }
                    }
                }
            }
            return R.ok("登录失败").put("tologin","http://127.0.0.1:5501/login.html");
        }else {//未登录，或者，跨服务登录了，让他去到sso系统鉴定
            String url="http://localhost:12000/user/member/toLogin" + "?url=http://127.0.0.1:5500/index.html";
            return R.error().put("url",url);
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		PatientTurnImgEntity patientTurnImg = patientTurnImgService.getById(id);

        return R.ok().put("patientTurnImg", patientTurnImg);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PatientTurnImgEntity patientTurnImg){
		patientTurnImgService.save(patientTurnImg);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PatientTurnImgEntity patientTurnImg){
		patientTurnImgService.updateById(patientTurnImg);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		patientTurnImgService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
